﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Framework.Controls;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.SelectQuery
{
    /// <summary>
    /// 
    /// </summary>
    public partial class frmSelectByXZQ : DevComponents.DotNetBar.Office2007Form
    {
        /// <summary>
        /// 
        /// </summary>
        FrmQuery m_frmQuery = null;
        ///// <summary>
        ///// 
        ///// </summary>
        //List<IFeatureLayer> m_List = null;
        /// <summary>
        /// 
        /// </summary>
        private IApplication m_Application = null;
        /// <summary>
        /// 
        /// </summary>
        public IApplication Application
        {
            set
            {
                m_Application = value;
            }
        }


        /// <summary>
        /// 绘制工具构建对象
        /// </summary>
        private ToolCreator _ToolCreator = null;

        /// <summary>
        /// 地图关联对象
        /// </summary>
        private IHookHelper m_HookHelp = null;

        /// <summary>
        /// 
        /// </summary>
        public frmSelectByXZQ()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSelectByXZQ_Load(object sender, EventArgs e)
        {
            //this.cbxLayer.Items.Clear();
            m_HookHelp = new HookHelper();
            m_HookHelp.Hook = (m_Application as IFramework).Object;

            List<ValueName> btns = new List<ValueName>()
            {
                new ValueName(){Value="ToolDrawRectangle",Name="绘制矩形"},
                new ValueName(){Value="ToolDrawPolygon",Name="绘制多边形"}
            };
            _ToolCreator = new ToolCreator(btnDrawExtent, m_HookHelp.Hook);
            _ToolCreator.HideForm = true;
            _ToolCreator.Create(btns, true);
            _ToolCreator.DrawFinished += OnDrawFinished;

            regionTree.Hook = m_HookHelp.Hook;
            
            layersComboBox1.ShapeType = zjgisShapeType.Line | zjgisShapeType.Point | zjgisShapeType.Polygon | zjgisShapeType.MultiPatch;
            layersComboBox1.OnCreate((m_Application as IFramework).MapControl.Object, (m_Application as IFramework).TOCControl.Object);
            if (layersComboBox1.Nodes.Count >0)
                layersComboBox1.SelectedIndex = 0;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (layersComboBox1.SelectedLayer == null) return;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                IFeatureLayer m_FeatureLayer = layersComboBox1.SelectedLayer as IFeatureLayer;

                IGeometry m_Geometry = regionTree.SelectedGeometry;
                if (m_frmQuery == null || m_frmQuery.IsDisposed)
                {
                    m_frmQuery = FrmQuery.Instance();
                    m_frmQuery.Application = m_Application;
                }
                if (m_frmQuery.Visible == false)
                    m_frmQuery.Show((m_Application as IFramework).MdiForm);
                if (m_frmQuery.SelectGeometry != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_frmQuery.SelectGeometry);
                }
                m_frmQuery.SetCurrentLayer(m_FeatureLayer as ILayer);
                m_frmQuery.SelectGeometry = m_Geometry;
                m_frmQuery.RefreshTreeView();
                
                this.Visible = false;
            }
            catch (System.Exception)
            {
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 绘制完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnDrawFinished(object sender, DrawArgs e)
        {
            regionTree.SelectRegion(e.Geometry, e.Shift);
        }

        private void frmSelectByXZQ_FormClosed(object sender, FormClosedEventArgs e)
        {
            (m_Application as IFramework).Tool = null;
            if (_ToolCreator != null) _ToolCreator.Clear();
            regionTree.ClearNodeCheck(regionTree.Nodes);
            regionTree.Clear();
        }


    }
}
